// pages/order/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:0,
    page:1,
    orderPrintloading:{},
    orderPrintdisabled:{},
    status:0,
    lastpage:0,
    listData:[],
    shop_list_select: [],
    shop_list: [],
    showError:0,
    popup:0,
    popup2: 0,
    popup3: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() })
    if (options.status){
      this.data.status = options.status;
      this.setData({ status: options.status });
    }
    app.getUserInfo(()=>{
      this.init();
    })
       
  },
  init:function(){
    var shop_info = wx.getStorageSync('shop_info');
    //  console.log("shop_info",shop_info);
    if (shop_info) {
      this.setData({ shop_info: shop_info });
      this.loadOderList();
      this.getShopList();
    } else {
      this.getShopList((shop_list) => {
        var shop_info = shop_list[0];
        this.saveShopInfo(shop_info);
        this.loadOderList();
      });
    } 
  },
  setStatus:function(e){
    var status = e.currentTarget.dataset.status;
    if (this.data.status == status){
      return ;
    }
    this.setData({ status: status});
    this.loadOderList(1);
    
  },
  //切换门店
  bindChange: function (e) {
    var value = e.detail.value;
    var shop_info = this.data.shop_list[value];
    this.setData({ shop_info: shop_info });
    wx.setStorageSync("shop_info", shop_info);
    this.loadOderList(1);
  },
//门店列表
  getShopList:function(cb){
    var that = this;
    app.api("mall/shop/get", {

    }, function (res) {
     
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length==0){
        app.alert("您当前账号下没有开通任何门店，请到官网创建门店后再试");
        that.setData({ showError:1});
        return ;
      }
      var shop_list = res.data.list;
      var shop_list_select =[];
      for (var i in shop_list) {
        shop_list_select.push(shop_list[i].name);
      }
      var shop_info = shop_list[0];
      // console.log("shop_list_select",shop_list_select);
      that.setData({
        shop_list: shop_list,
        shop_list_select: shop_list_select,
      }); 
      cb ? cb(shop_list):'';
      
    })
  },


//缓存门店信息
  saveShopInfo(shop_info){
    this.setData({
      shop_info: shop_info
    });
    wx.setStorageSync("shop_info", shop_info);
  },
 //订单列表 
  loadOderList:function(reload){
    var that = this;
    if (reload == 1) {
      this.setData({
        loading: 0,
        page: 1,
        lastpage: 0,
        showError:0,
        listData: [],
      });
    }

    var page = this.data.page;
    if (this.data.loading == 1 || this.data.lastpage == 1) {
      //console.log(this.data);
      return;
    }
    this.data.loading = 1;
    app.api("mall/order/get", {
      page: page,
      limit: 10,
      shop_id:this.data.shop_info.id,
      status: this.data.status,
      order: 'id desc'
    }, function (res) {
      that.setData({ showError: 1 });
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
      that.data.page++;
      that.setData({
        listData: that.data.listData.concat(res.data.list),
      });
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // app.setPageData("pages/index/index", { reload: 1 });
    var reload = app.getPageData("pages/index/index","reload");
    if (reload==1){
      this.loadOderList(1);
      app.setPageData("pages/index/index", { reload: 0 });
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadOderList(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadOderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
//确认接单
  updateStatus_6(e){
    var order_sn = e.currentTarget.dataset.order_sn;
    var that = this;
    app.api('/mall-admin/mall-order/updateStatus_6', {
            order_sn: order_sn,
      shop_id: this.data.shop_info.id
        }, function(data) {
            if (data.error == 0) {
              wx.showToast({
                title: '操作成功',
              })
              that.loadOderList(1);
            } else {
                app.alert(data.msg);
            }
        })
  },

  //配送完成
  updateStatus_4(e) {
    var order_sn = e.currentTarget.dataset.order_sn;
    var that = this;
    app.api('/mall-admin/mall-order/updateStatus_4', {
      order_sn: order_sn,
      shop_id:this.data.shop_info.id
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.loadOderList(1);
      } else {
        app.alert(data.msg);
      }
    })
  },

   //立即配送
  deliverMallOrder(e) {
    var order_sn = e.currentTarget.dataset.order_sn;
    this.setData({ order_sn: order_sn});
    var that = this;
    var itemListArray = [];
    itemListArray.push({ text: '自营配送', act: function(){
      that.setData({ popup: 1})
    } });
    itemListArray.push({
      text: '蜂鸟配送', act: function () {
        that.setData({ popup2: 1, type: 'ele'})
      } });
    itemListArray.push({
      text: '达达配送', act: function () {
        that.setData({ popup2: 1, type: 'dada' })
      }
    });
    var itemList = [];
    //console.log(itemListArray);
    for (var i in itemListArray) {
      itemList.push(itemListArray[i].text);
    }
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        console.log(itemListArray[res.tapIndex]);
        itemListArray[res.tapIndex]['act'](e);
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  btn_cancel:function(){
    this.setData({
      popup:0,
      loading: 0,
      disabled: 0
    })
  },
  btn_cancel2: function () {
    this.setData({
      popup2: 0,
      loading:0,
      disabled:0
    })
  },
  btn_cancel3: function () {
    this.setData({
      popup3: 0,
      loading: 0,
      disabled: 0
    })
  },
  show_popup3:function(e){
    var order_sn = e.currentTarget.dataset.order_sn;
    this.setData({
      popup3: 1,
      order_sn: order_sn,
      refund_money: e.currentTarget.dataset.refund_money
    })
  },
  
  //自己配送
  formSubmit:function(e){
    var post = e.detail.value;
    var that = this;
    that.setData({
      loading:1,
      disabled:1
    });
    app.api('/mall-admin/mall-order-deliver/add', {
      order_sn: this.data.order_sn,
      remark: post.remark,
      tracking_number: post.tracking_number,
      shop_id: this.data.shop_info.id,
      method: 'POST',
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.btn_cancel();
        that.loadOderList(1);
      } else {
        app.alert(data.msg);
        that.setData({
          loading: 0,
          disabled: 0
        });
      }
    })
  },

  //蜂鸟配送
  formSubmit2: function (e) {
    var post = e.detail.value;
    var that = this;
    that.setData({
      loading: 1,
      disabled: 1
    });
    if (this.data.type == 'ele') {
      var url = '/mall-admin/mall-order/delivery';
    } else {
      var url = '/mall-admin/mall-order/delivery_dada';
    }
    app.api(url, {
      order_sn: this.data.order_sn,
      remark: post.remark,
      order_weight: post.order_weight,
      shop_id: this.data.shop_info.id,
      method: 'POST',
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.btn_cancel2();
        that.loadOderList(1);
      } else {
        app.alert(data.msg);
        that.setData({
          loading: 0,
          disabled: 0
        });
      }
    })
  },



  //退款
  updateStatus_16: function (e) {
    var post = e.detail.value;
    var that = this;
    that.setData({
      loading: 1,
      disabled: 1
    });
    if (post.remark==""){
      app.alert('请输入备注!');
      return ;
    }
    if (post.refund_money == "") {
      app.alert('请输入退回金额!');
      return;
    }
    app.api('/mall-admin/mall-order/updateStatus_16', {
      order_sn: this.data.order_sn,
      refund_desc: post.remark,
      refund_money: post.refund_money,
      shop_id: this.data.shop_info.id,
    //  method: 'POST',
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.btn_cancel3();
        that.loadOderList(1);
      } else {
        app.alert(data.msg);
        that.setData({
          loading: 0,
          disabled: 0
        });
      }
    })
  },
  
  //打印小票
  orderPrint(e){
    var order_sn = e.currentTarget.dataset.order_sn;
    var that = this;
    var tmp ={};
    tmp[order_sn] =1;  
    this.setData({
      orderPrintloading: tmp ,
      orderPrintdisabled: tmp
    });
    app.api('/mall-admin/mall-order/orderPrint', {
      order_sn: order_sn,
      shop_id: this.data.shop_info.id
    }, function (data) {
      tmp[order_sn]=0;
      that.setData({
        orderPrintloading: tmp,
        orderPrintdisabled: tmp
      });
      if (data.error == 0) {
        wx.showToast({
          title: '打印指令发送成功',
        })
      } else {
        app.alert(data.msg);
      }
    })
  },

})